var
    form = $('#test-form'),
    langs = form.find('[name=lang]'),           //所有input节点
    selectAll = form.find('label.selectAll :checkbox'), //选择框  
    selectAllLabel = form.find('label.selectAll span.selectAll'),       //全选标签
    deselectAllLabel = form.find('label.selectAll span.deselectAll'), //不全选标签
    invertSelect = form.find('a.invertSelect');             //反选

// 重置初始化状态:
form.find('*').show().off();
form.find(':checkbox').prop('checked', false).off();
deselectAllLabel.hide();
// 拦截form提交事件:
form.off().submit(function (e) {
    e.preventDefault();
    alert(form.serialize());
});
// TODO:绑定事件
// function selectAll_Display(){
//     deselectAllLabel.show();
//     selectAllLabel.hide()
// }
// function deselectAll_Display(){
//     deselectAllLabel.hide();
//     selectAllLabel.show()
// }


// selectAll.change(function(){
//     if(selectAll.is(':checked')){
//         selectAll_Display();    //全选
//         langs.prop('checked',true)
//     }
//     else{
//         deselectAll_Display(); //全不选
//         langs.prop('checked',false)
//     }
// })

selectAll.click(function() {
    if(selectAll.is(':checked')) {
        selectAllLabel.hide();
        deselectAllLabel.show();
        langs.prop('checked',true);
    }
    else {
        deselectAllLabel.hide();
        selectAllLabel.show();
        langs.prop('checked',false);
    }
});

langs.change(function() {
    let tmp = true;
    for(var i = 0 ; i < langs.length ; i++){
        if(!langs[i].checked) tmp = false;
    }        
    if(tmp) {
        selectAll.prop('checked',true);
        selectAllLabel.hide();
        deselectAllLabel.show();
    }
    else {
        selectAll.prop('checked',false);
        deselectAllLabel.hide();
        selectAllLabel.show();
    }
});

invertSelect.click(function() {
    for(var i = 0 ; i < langs.length ; i++){
        langs[i].checked = !langs[i].checked;
    }
    langs.change();
});
